import { Component, OnInit } from '@angular/core';
import { ConfirmationService, MessageService } from 'primeng/api';
import { LicenseService } from 'src/app/service/license.service';
import { transform } from 'src/app/service/utils/transform';
import { select, Store } from '@ngrx/store';
import { getApplicant } from 'src/app/store/utils/utils.reducer';

@Component({
    selector: 'app-license-manage',
    templateUrl: './license-manage.component.html',
    styleUrls: ['./license-manage.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class LicenseManageComponent implements OnInit {
    editTag: boolean = false;
    display: boolean = false;
    data: any[];
    currentItem = { id: null };
    departments: any;
    licenseStates: any;
    applicant: any;
    constructor(
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        private licenseService: LicenseService,
        private transform: transform,
        private store: Store
    ) {
        this.departments = this.transform.departments;
        this.licenseStates = this.transform.licenseStates;
    }
    async fetchLicense() {
        const res = await this.licenseService.fetchLicenseByConfig({
            applicantName: null,
            end: null,
            license: null,
            start: null,
        });
        console.log(res);
        this.data = res;
    }
    addForm = {
        applicantId: '',
        available: '',
        departmentId: '',
        license: '',
        licenseVolume: '',
        startTime: new Date(),
        id:null
    };
    blankForm = {
        applicantId: '',
        available: '',
        departmentId: '',
        license: '',
        licenseVolume: '',
        startTime: new Date(),
        id:null
    };
    transDateTimeToDate = (d) => {
        if (!d) {
            return '';
        }
        if (typeof d === 'string') {
            return d;
        }
        const a = d;
        return a.getFullYear() + '-' + (a.getMonth() + 1) + '-' + a.getDate();
    };
    async add() {
        const res = await this.licenseService.addLicenseByConfig({
            ...this.addForm,
            startTime: this.transDateTimeToDate(this.addForm.startTime),
        });
        if (res.code == 200) {
            this.fetchLicense();
        }
    }
    async edit() {
        console.log(this.addForm)
        const res = await this.licenseService.editLicenseByConfig({
            ...this.addForm,
            startTime: this.transDateTimeToDate(this.addForm.startTime),
        });
        if (res.code == 200) {
            this.fetchLicense();
            //modalState.value = false;
        }
    }
    submit(item?) {
        if (this.editTag === false) {
            this.add();
            this.display = false;
        } else {
            this.edit();
            this.display = false;
        }
    }
    async delete(item) {
        const res = await this.licenseService.deleteLicenseById({
            licenseId: item.id,
        });
        if (res.code == 200) {
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: '你已经删除',
            });
            this.fetchLicense();
        }
    }
    ngOnInit(): void {
        this.store.pipe(select(getApplicant)).subscribe((data) => {
            this.applicant = data;
            console.log(this.applicant);
        });
        console.log(this.applicant);
        this.fetchLicense();
    }
    openDialog() {
        this.display = true;
        this.editTag = false;
        this.addForm = {
            ...this.blankForm,
            startTime: new Date(),
        };
    }
    openDialog2(item) {
        this.display = true;
        let a = item.startTime;
        let b = item.applicantId;
        a = new Date(a);
        console.log(a);
        this.addForm = {
            ...item,
            startTime: a,
            applicantId: b,
        };
        this.editTag = true;
    }
    deleteLicense(item: any) {
        //console.log("ok")
        this.confirmationService.confirm({
            message: '请问你确认删除吗 ?',
            header: '警告',
            icon: 'pi pi-exclamation-triangle',
            accept: () => {
                this.delete(item);
            },
        });
    }
}
